//
//  XDBackMoneyViewController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/10.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDBackMoneyViewController.h"
#import "AFHTTPSessionManager+POST.h"
#import "XDBackMoneyTableViewCell.h"
#import "XDBackMoneyModel.h"
#import "MJExtension.h"
@interface XDBackMoneyViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *backArr;

@property (nonatomic, weak) UITableView *tableView;
@end

@implementation XDBackMoneyViewController

- (NSArray *)backArr {
    if (_backArr == nil) {
        _backArr = [NSArray array];
    }
    return _backArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadData];
    [self setupUI];
}

- (void)loadData {
    
    NSDictionary * param = @{@"method":@"investmentPlanInfo",@"investId":[NSString stringWithFormat:@"%zd",self.investId],@"page":@"0"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            
            /*
             
             {
             code = 100000;
             message = "\U64cd\U4f5c\U6210\U529f";
             result =     {
             myInvestmentPlanList =         (
             {
             currentBackNumbe = 1;
             payBackNumber = 1;
             payEarnings = "0.7";
             payMoney = 111;
             payTime = "2017-02-17 14:41:19";
             status = "\U5f85\U6536\U6b3e";
             }
             );
             totalPages = 1;
             };
             type = success;
             }

             */
            self.backArr = [XDBackMoneyModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"myInvestmentPlanList"]];
            
            [self.tableView reloadData];
            
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
            
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)navigationBarCof {
    self.title = @"回款计划";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:kColorRGB(187,147,115) ,NSFontAttributeName:kBaseFont_(19)}];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self navigationBarCof];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)setupUI {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    tableView.tableFooterView = view;
    
}

#pragma mark -- table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.backArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *myID = @"XDBackMoneyTableViewCell";
    XDBackMoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myID];
    if (cell == nil) {
        cell = [[XDBackMoneyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myID];
    }
    cell.model = self.backArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

@end
